package com.nit.store.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nit.store.mapper.ProductMapper;
import com.nit.store.pojo.dto.output.AddressDto;
import com.nit.store.pojo.dto.output.ProductDto;
import com.nit.store.pojo.entity.Product;
import com.nit.store.service.ProductService;
import com.nit.store.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper produceMapper;

    /**
     * 热销排行榜
     *
     * @return
     */
    @Override
    public List<ProductDto> getHotList() {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1).orderByDesc("priority").last("limit 0,4");
        List<Product> data = produceMapper.selectList(wrapper);
        return CommonUtils.getMapperFacade().mapAsList(data, ProductDto.class);
    }

    @Override
    public List<ProductDto> getProduct(Long id) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        List<Product> data = produceMapper.selectList(wrapper);
        return CommonUtils.getMapperFacade().mapAsList(data, ProductDto.class);
    }

    /**
     * 根据key查找商品
     * @param key
     * @return
     */
    @Override
    public List<ProductDto> getProductByKey(String key) {
        System.out.println("key===" + key);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.like("title",key).or()
        .like("sell_point",  key ).or()
        .like("item_type", key);
        List<Product> data = produceMapper.selectList(wrapper);
        return CommonUtils.getMapperFacade().mapAsList(data, ProductDto.class);
    }
}
